package com.game.fighting.component;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.physics.HitBox;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.almasb.fxgl.time.LocalTimer;
import com.game.fighting.config.ArcheryType;
import com.game.fighting.config.ConfigData;
import javafx.util.Duration;


public class BowComponent extends Component {
    private AnimatedTexture at;
    private LocalTimer shootTimer;
    private Duration shootDelay = Duration.seconds(ConfigData.SHOOT_DELAY);
    public BowComponent() {
        at = new AnimatedTexture(new AnimationChannel(FXGL.image("bow.png"),
                Duration.seconds(0.5), 4));
    }

    @Override
    public void onAdded() {
        shootTimer = FXGL.newLocalTimer();
        entity.getViewComponent().addChild(at);
        entity.getBoundingBoxComponent().addHitBox(
                new HitBox(BoundingShape.box(686/4.0,247)));
    }

    public void shoot() {
        if (shootTimer.elapsed(shootDelay)) {
            Entity shootBar = FXGL.getGameWorld().getSingleton(ArcheryType.SHOOT_BAR);
            ShootBarComponent shootBarComponent = shootBar.getComponent(ShootBarComponent.class);
            shootBarComponent.reset();
            FXGL.play("shoot.wav");
            at.play();
            FXGL.spawn("arrow",entity.getCenter().subtract(99/2.0, 11/2.0));
            shootTimer.capture();
        }
    }
}
